/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var PresentationController = new Class({
    
    Implements: [Events],
    
    sc   : null,
    dfc  : null,
    curr : null,
    real : null,
    sd   : null,
    
    initialize: function(options){
        this.sc  = options.sc;
        this.dfc = options.dfc;
        this.sd  = new SlidingDoors({
            'el' : $('screen')
        });
    },
    
    start: function(){      
        var mainLoop = function(slide){ 
            //remove events
            slide.removeEvents();
            //console.log('layout frame:'+frame);
            slide.setHasSibling(this.sc.countSlides() > 1);
            //add events
            slide.addEvent('timelineEnd', function(slide){  
                slide.removeEvents('timelineEnd');
                //fire event that slide is about to be unloaded
                ads_eo.fireEvent('frameUnload', null, [slide]);
                slide.finalize(); 
                //fire event
                this.fireEvent('end');
                //keep loop going
                this.sc.shift();
                mainLoop(this.sc.getCurrentSlide());
            }.bind(this));
            slide.addEvent('checkDescriptor', function(){                
                this.fireEvent('checkDescriptor');
            }.bind(this));
            var renderSlide = function(slide){
                slide.removeEvents('contentLoaded');               
                slide.render();  
                this.sd.slide();
            }.bind(this);
            this.sd.addEvent('end', function(slidingDoors){ 
                this.sd.removeEvents();
                if(this.curr.hasSibling){
                    this.curr = this.sc.getNextSlide();
                    this.curr.load(this.sd.getCurrent());
                }
            }.bind(this));
            //load frame
            if(this.curr == null){
                slide.addEvent('contentLoaded', renderSlide);
                slide.load(this.sd.getCurrent()); 
            }else{
                renderSlide(slide);
            }
            this.curr = this.real = slide; 
        }.bind(this);
        mainLoop(this.sc.getFirstSlide());        
    },
    
    slide: function(){
        this.real.fireEvent('timelineEnd',[this.curr]);
    },
    
    getCurrentSlide: function(){
        return this.curr;
    },
    
    getRealSlide: function(){
        return this.real;
    }
});


